package com.boot.ch7.aop;

import com.boot.ch7.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.boot.ch7.annotation.CustomInterceptor;
import com.boot.ch7.service.HandleService;

/**
 * @author onlyone
 */
@Aspect
@Component
public class CustomAOP {

    @Autowired
    private HandleService handleService;

    /**
     * 拦截@CustomInterceptor标记的方法，并进行环绕增强处理
     */
    @Around("@annotation(customInterceptor)")
    public Object around(JoinPoint joinPoint, CustomInterceptor customInterceptor) throws Throwable {
        Object[] params = joinPoint.getArgs();
        String param = (String) params[0];

        if (StringUtils.isBlank(param)) {
            throw new ServiceException("-1", "praram参数不能为空");
        }

        String handleResult = handleService.handle(param);
        System.out.println(handleResult);

        Object result = ((ProceedingJoinPoint) joinPoint).proceed();
        return result;
    }
}
